package com.application.meizhi.ui.activity;


import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;

import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.application.meizhi.R;
import com.application.meizhi.persenter.MainActivityPresenter;
import com.application.meizhi.ui.fragment.douban.DouBanViewPagerFragment;
import com.application.meizhi.ui.iView.IMainView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity<MainActivityPresenter>
        implements NavigationView.OnNavigationItemSelectedListener, IMainView {


    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer;

    @BindView(R.id.nav_view)
    NavigationView navigationView;

    private int currentTabIndex;

    private FragmentManager fm;
    private FragmentTransaction transaction;

    private DouBanViewPagerFragment mDouBan;

    private List<Fragment> fragments;

    private int index;

    @Override
    protected void initPresenter() {

        presenter = new MainActivityPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();


        if (id == R.id.nav_douban) {
            changIndex(0, "豆瓣妹子", item);
        } else if (id == R.id.nav_jiandan) {
            changIndex(1, "煎蛋妹子", item);
        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void initView() {


        toolbar.setTitle("豆瓣妹子");
        setSupportActionBar(toolbar);


        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setCheckedItem(R.id.nav_douban);

        addFragments();

        setDefaultFragment();


    }

    private void addFragments() {

        fragments = new ArrayList<>();
        if (mDouBan == null) {
            mDouBan = new DouBanViewPagerFragment();
        }
        fragments.add(mDouBan);
    }

    private void setDefaultFragment() {
        fm = getSupportFragmentManager();
        transaction = fm.beginTransaction();
        transaction.replace(R.id.content_main, mDouBan);
        transaction.commit();

    }

    public void changIndex(int changNum, String title, MenuItem item) {

        index = changNum;
        switchFragment();
        item.setChecked(true);
        toolbar.setTitle(title);
        drawer.closeDrawers();
    }


    public void switchFragment() {

        transaction = fm.beginTransaction();
        transaction.hide(fragments.get(currentTabIndex));
        if (!fragments.get(index).isAdded()) {
            transaction.add(R.id.content_main, fragments.get(index));
        }
        transaction.show(fragments.get(index)).commit();
        currentTabIndex = index;
    }
}
